<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input
        v-model="query.carNumber"
        clearable
        placeholder="请输入内容"
        class="search-main"
      />
      <span class="search-label">车主姓名：</span>
      <el-input
        v-model="query.personName"
        clearable
        placeholder="请输入内容"
        class="search-main"
      />
      <span class="search-label">状态：</span>
      <el-select v-model="query.cardStatus">
        <el-option
          v-for="item in cardStatusList"
          :key="item.id"

          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button
        type="primary"
        class="search-btn"
        @click="serachFn"
        v-auth-dir="`parking:card:query`"
      >查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button v-auth-dir="`parking:card:add_edit`" type="primary" @click="$router.push('/add-card')">添加月卡</el-button>
      <el-button v-auth-dir="`parking:card:remove`" @click="dels">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="carList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55px" />
        <el-table-column type="index" label="序号" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column
          label="状态"
          :formatter="formatetrFn"
          prop="cardStatus"
        />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scoped">
            <el-button size="mini" v-auth-dir="`parking:card:recharge`" type="text" @click="openCardWindows('renow',scoped.row.id)">续费</el-button>
            <el-button size="mini" v-auth-dir="`parking:card:query`" type="text" @click="openCardWindows('check',scoped.row.id)">查看</el-button>
            <el-button size="mini" v-auth-dir="`parking:card:add_edit`" type="text" @click="openCardWindows('edit',scoped.row.id)">编辑</el-button>
            <el-button size="mini" v-auth-dir="`parking:card:remove`" type="text" @click="del(scoped.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next, sizes"
        :page-size="query.pageSize"
        :page-sizes="[5,10,20,50]"
        :total="total"
        @current-change="handlePage"
        @size-change="handleSizeChange"
      />
    </div>
  </div>
</template>

<script>
import { getCardListAPI, delCardAPI } from '@/apis/card'
export default {
  data() {
    return {
      query: {
        page: 1,
        pageSize: 5,
        carNumber: '',
        personName: '',
        cardStatus: ''
      },
      carList: [],
      total: 0,
      cardStatusList: [
        {
          value: '',
          label: '全部'
        },
        {
          value: '0',
          label: '可用'
        },
        {
          value: '1',
          label: '已过期'
        }
      ],
      selectedCarList: [],
      type: ''
    }
  },
  computed: {
    // total: ''
  },
  created() {
    this.getCardList()
  },
  methods: {
    // 获取数据列表
    async getCardList() {
      const res = await getCardListAPI(this.query)
      // console.log(res)
      this.carList = res.data.rows
      this.total = res.data.total
    },
    // 修改 可用
    formatetrFn(row) {
      const keyObj = {
        0: '可用',
        1: '已过期'
      }
      return keyObj[row.cardStatus]
    },
    // 修改当前页码
    handlePage(page) {
      this.query.page = page
      this.getCardList()
    },
    // 修改当前页信息条数
    handleSizeChange(pageSize) {
      this.query.pageSize = pageSize
      this.getCardList()
    },
    // 查询功能
    serachFn() {
      this.query.page = 1
      this.getCardList()
    },
    // 编辑月卡
    edit(id) {
      // console.log(id)
      this.$router.push(`/add-card?id=${id}`)
    },
    // 删除月卡
    async del(id) {
      this.$confirm('你确定要删除这条数据吗？', '提示', {
        type: 'warning'
      }
      ).then(async() => {
        await delCardAPI(id)
        // console.log(res)
        this.getCardList()
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      }).catch(() => {
        this.$message('取消操作')
      })
    },
    // 选中对象
    handleSelectionChange(val) {
      this.selectedCarList = val
    },
    // 批量删除
    dels() {
      const abs = this.selectedCarList.map(item => item.id)
      // console.log(abs.join(','))
      if (this.selectedCarList.length <= 0) {
        this.$message({
          message: '没有选中内容,请重新选择',
          type: 'warning'
        })
        return
      }
      this.$confirm('你确定要批量删除这些数据吗？', '提示', {
        type: 'warning'
      }
      ).then(async() => {
        await delCardAPI(abs)
        // console.log(res)
        this.getCardList()
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      }).catch(() => {
        this.$message('取消操作')
      })
      this.getCardList
    },
    // 处理月卡功能窗口
    openCardWindows(type, id) {
      this.$router.push(`/add-card?id=${id}&type=${type}`)
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>
